package org.csu.mypetstore.web.servlets;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.util.ResultItemList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class UpdateItemsByProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String productId = request.getParameter("productId");
        CatalogService service = new CatalogService();
        Product product = service.getProduct(productId);
        List<Item> itemList = service.getItemListByProduct(productId);

        //方式一
//        HttpSession session = request.getSession();
//        session.setAttribute("itemList", itemList);
//
//        System.out.println(session.getAttribute("itemList"));
        //方式二
        response.setContentType("text/json");
        PrintWriter out = response.getWriter();

        ResultItemList resultItemList = new ResultItemList();

        resultItemList.setItemList(itemList);

        String rt = JSON.toJSONString(resultItemList);

        out.print(rt);

        out.flush();
        out.close();
    }
}
